package com.qf.web.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.blog.dto.SiteInfoDTO;
import com.qf.api.blog.entity.SiteInfoEntity;
import com.qf.api.blog.service.SiteInfoService;
import com.qf.api.blog.vo.SiteInfoVO;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.core.constant.MinioConstant;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import com.qf.core.utils.FileUtils;
import com.qf.minio.service.MinioService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: sin
 * @Date: 2025/5/27 10:19
 * @Description:
 **/
@Tag(name = "01.站点管理")
@ApiOperationLogTitle("站点模块")
@RestController
@RequestMapping("/admin/siteInfo")
public class AdminSiteController extends BaseController {

    @Resource
    private SiteInfoService siteInfoService;

    @Resource
    private MinioService minioService;

    @GetMapping
    public ResponseResult<SiteInfoVO> getSiteInfo() {
        SiteInfoEntity siteInfo = siteInfoService.getById(1);
        return ResponseResult.success(BeanUtil.copyProperties(siteInfo, SiteInfoVO.class));
    }

    @PutMapping("/introduction")
    @ApiOperationLog(desc = "更新个人简历",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> editIntroduction(@RequestBody String introduction) {
        LambdaUpdateWrapper<SiteInfoEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SiteInfoEntity::getId, 1L) // 假设根据 ID 修改
                .set(SiteInfoEntity::getIntroduction, introduction); // 修改指定字段
        return isOk(siteInfoService.update(updateWrapper));
    }

    @PutMapping
    @ApiOperationLog(desc = "更新网站信息",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> editSiteInfo(@RequestBody SiteInfoDTO siteInfoDTO) {
        SiteInfoEntity siteInfo = BeanUtil.copyProperties(siteInfoDTO, SiteInfoEntity.class);
        siteInfo.setId(1L);
        return isOk(siteInfoService.updateById(siteInfo));
    }

    @PostMapping(value = "/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult<Boolean> uploadAvatar(@Parameter(description = "头像文件") @RequestPart("avatar") MultipartFile avatar) {
        MultipartFile multipartFile = FileUtils.compressImageToMultipartFile(avatar, FileUtils.IMAGE_SCALE, FileUtils.IMAGE_QUALITY);
        String url = minioService.uploadFile(multipartFile, MinioConstant.BUCKET_NAME_AVATAR, FileUtils.generateUniqueFileName(multipartFile.getOriginalFilename()));
        LambdaUpdateWrapper<SiteInfoEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SiteInfoEntity::getId, 1L) // 假设根据 ID 修改
                .set(SiteInfoEntity::getAvatar, url); // 修改指定字段
        return isOk(siteInfoService.update(updateWrapper));
    }

    @PostMapping(value = "/bgImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult<Boolean> uploadBgfImage(@Parameter(description = "背景图") @RequestPart("bgImage") MultipartFile avatar)  {
        MultipartFile multipartFile = FileUtils.compressImageToMultipartFile(avatar, FileUtils.IMAGE_SCALE, FileUtils.IMAGE_QUALITY);
        String url = minioService.uploadFile(multipartFile, MinioConstant.BUCKET_NAME_AVATAR, FileUtils.generateUniqueFileName(multipartFile.getOriginalFilename()));
        LambdaUpdateWrapper<SiteInfoEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SiteInfoEntity::getId, 1L) // 假设根据 ID 修改
                .set(SiteInfoEntity::getBgImage, url); // 修改指定字段
        return isOk(siteInfoService.update(updateWrapper));
    }

}
